<%@ page language="java" pageEncoding="utf-8"%>
<%@page import="com.farm.web.constant.FarmConstant"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="/view/conf/farmtag.tld" prefix="PF"%>
<script src="text/lib/ace/ace.js" type="text/javascript" charset="utf-8"></script>
<pre id="editor"
	style="padding: 0px; margin: 0px; height: 100%; border: 0px; width: 100%;"></pre>

<c:if test="${template.pstate=='9'}">
	<script type="text/javascript">
		//只读模式
		var onlyRede = true;
	</script>
</c:if>
<c:if test="${template.pstate!='9'}">
	<script type="text/javascript">
		//编辑模式
		var onlyRede = false;
	</script>
</c:if>

<script>
	var cbtg_editor;
	$(function() {
		cbtg_editor = ace.edit("editor", {
			theme : "ace/theme/tomorrow_night_eighties",
			mode : "ace/mode/html",
			wrap : true,
			autoScrollEditorIntoView : true
		});
		cbtg_editor.setTheme("ace/theme/twilight");
		// 添加事件监听器
		window.addEventListener('resize', handleResize);
		//保存
		document.addEventListener('keydown', function(event) {
			// 检查是否按下了Ctrl+S组合键
			if ((event.ctrlKey || event.metaKey) && event.key === 's') {
				event.preventDefault(); // 阻止默认行为
				saveRemoteText(cbtg_template_file_id);
			}
		});

        // 监听光标移动事件
        cbtg_editor.on('changeSelection', function(e) {
        	var session = cbtg_editor.getSession();
            var selection = session.getSelection();
            var range = selection.getRange();
            // 获取光标开始位置
            var startPosition = range.start;
            var startRow = startPosition.row;
            var startColumn = startPosition.column;
            $('#focusInfoId').html("当前"+(startRow+1)+"行|"+startColumn+"列");
        });
		cbtg_editor.getSession().on('change', editorChange);
		cbtg_editor.session.setMode("ace/mode/java" );
		cbtg_editor.setValue("模板名称：'${template.name }'\n模板备注：'${template.pcontent }'\n模板状态：${template.pstate }\n创建时间：${template.ctime }\n\n当前登录用户：'${USEROBJ.name }'");
		cbtg_editor.setReadOnly(true); 
	});
	//編輯器修改事件
	function editorChange() {
		$('#fileStateId').html("已修改");
		$('#filestatInfoId').html("共有"+cbtg_editor.session.getLength()+"行");
	}

	// 窗口改变大小
	function handleResize() {
		cbtg_editor.resize();
	}
	
	//重新加载文件
	function reLoadRemoteText(tfileid){
		if(confirm("确定重新加载数据？")){
			loadEditorText(tfileid);
		}
	}
	// 向编辑器加载文件
	function loadEditorText(tfileid) {
		//清理
		cbtg_editor.setValue("数据加载中...");
		cbtg_editor.setReadOnly(true);
		//只读
		//加载远程文件
		$.post('temveiw/loadFileText.do?templateFileId=' + cbtg_template_file_id, {},
				function(flag) {
					//可编辑
					cbtg_editor.setReadOnly(false);
					cbtg_editor.setValue(flag.tfile.text);
					cbtg_editor.session.setMode("ace/mode/" + flag.exname);
					cbtg_editor.gotoLine(1);
					$('#fileStateId').html("");
					cbtg_editor.resize();
					if(onlyRede){
						//只读模式
						cbtg_editor.setReadOnly(true);
					}
				}, 'json');
	}
	// 将编辑器内容存储
	function saveRemoteText(tfileid) {
		$('#fileSaveButtonId').html("保存中...");
		$.post('temveiw/saveFileText.do', {
			'templateFileId' : cbtg_template_file_id,
			'text' : cbtg_editor.getValue()
		}, function(flag) {
			if (flag.STATE == 0) {
				$('#fileSaveButtonId').html("保存成功!");
				setTimeout(function() {
					$('#fileSaveButtonId').html("保存");
				}, 1000);
				$('#fileStateId').html("");
			} else {
				alert(flag.MESSAGE);
			}
		}, 'json');
	}
</script>
<c:if test="${template.pstate=='9'}">
	<script type="text/javascript">
		//存档模式不允许保存文件
		function saveRemoteText(tfileid) {
		}
	</script>
</c:if>